package load.ogp;

import core.*;

/**
 *
 * @author Ben
 */
public class Cache extends StdNode{
    private InPort value = new InPort(Object.class);
    private DispatcherPort cachedChanged = new DispatcherPort();
    private boolean valid;
    private Object internalValue;

    public Cache() {
         valid = false;
         internalValue = null;
         autoPutPorts();
    }

    private TriggerPort valueChanged = new TriggerPort() {
            @Override
            public void callInternal() {
                valid = false;
            }
    };

    private OutPort cached = new OutPort(java.lang.Object.class, cachedChanged) {
        @Override
        protected Object getValueInternal() {
            if(!valid){
                internalValue = value.getValue();
                valid = true;
            }
            return internalValue;
        }
    };
}
